package utils;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 封装base64、16进制编码解码、散列加密工具类
 */
public class PasswordUtil {

    private static final SecureRandomNumberGenerator saltGenerator = new SecureRandomNumberGenerator();
    public static final String SHA1 = "SHA-1";
    public static final Integer ITERATIONS = 131;

    /**
     * @Description: HEX-byte[]--String转换
     * @param input 输入数组
     * @Return String
     */
    public static String encodeHex(byte[] input){
        return Hex.encodeToString(input);
    }

    /**
     * @Description: HEX-String--byte[]转换
     * @param input 输入字符串
     * @Return byte数组
     */
    public static byte[] decodeHex(String input){
        return Hex.decode(input);
    }

    /**
     * @Description: Base64-byte[]--String转换
     * @param input 输入数组
     * @Return String
     */
    public static String encodeBase64(byte[] input){
        return Base64.encodeToString(input);
    }

    /**
     * @Description: Base64-String--byte[]转换
     * @param input 输入字符串
     * @Return byte数组
     */
    public static byte[] decodeBase64(String input){
        return Base64.decode(input);
    }

    /**
      * @Description: SHA-1加密密码
      * @Param: [password]
      * @Return: java.util.Map<salt,encrypt_password>
      * @: Bink
      * @DateTime: 2021/6/9 4:17 下午
     **/
    public static Map<String, String> encryptPassword(String password){
        final String salt = saltGenerator.nextBytes().toHex();
        final String encrypt_password = new SimpleHash(SHA1, password, salt, ITERATIONS).toString();
        HashMap<String, String> map = new HashMap<>();
        map.put("salt", salt);
        map.put("password", encrypt_password);
//        return Map.of("salt", salt, "password", encrypt_password);
        return map;
    }


}
